package com.smu.recognize.controller;

import com.smu.recognize.Util.CurrentTimeUtil;
import com.smu.recognize.model.Admin;
import com.smu.recognize.model.AttConfig;
import com.smu.recognize.model.common.JsonResult;
import com.smu.recognize.model.common.JsonReturn;
import com.smu.recognize.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.smu.recognize.controller.adminController.CommonAction.getUserFromSession;

//RestContrller和Controller
/*
* 1) 如果只是使用@RestController注解Controller，则Controller中的方法无法返回jsp页面，或者html，
* 配置的视图解析器 InternalResourceViewResolver不起作用，返回的内容就是Return 里的内容。

2) 如果需要返回到指定页面，则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
    如果需要返回JSON，XML或自定义mediaType内容到页面，则需要在对应的方法上加上@ResponseBody注解。
* */
@Slf4j
@RestController
@RequestMapping("/config")
public class ConfigController {
    boolean flag;
    JsonResult jsonResult = new JsonResult();
    JsonReturn jsonReturn = new JsonReturn();
    @Autowired
    ConfigService service;

    @RequestMapping("/insert")
    public JsonReturn insert(AttConfig attConfig, HttpServletRequest request) {
        Admin admin = getUserFromSession(request);
        if (admin == null) {
            attConfig.setCreate_by("默认用户");
        } else {
            attConfig.setCreate_by(admin.getName());
        }
        attConfig.setCreate_time(CurrentTimeUtil.getTime());
        attConfig.setStatus(0);
        flag = service.insert(attConfig);
        return new JsonReturn(flag);
    }

    @RequestMapping("/delete")
    public JsonReturn delete(Integer c_id) {
        List<AttConfig> list=new ArrayList<>();
        list=service.findAll();
        for(AttConfig config:list){
            if (config.getC_id()==c_id){
                if (config.getStatus()==1){
                    return new JsonReturn(false,1);
                }
            }
        }
        flag = service.delete(c_id);
        return new JsonReturn(flag);
    }

    @RequestMapping("/all")
    public JsonReturn selectAll(HttpServletRequest request) {
        List<AttConfig> list = new ArrayList<>();
        list = service.findAll();
        jsonReturn.setData(list);
        jsonReturn.setSuccess(true);
        jsonReturn.setMsg("查询成功");
        jsonReturn.setCode(0);

        return jsonReturn;
    }

    @RequestMapping("updateStatus")
    public JsonReturn updateStatus(HttpServletRequest request,Integer c_id) {
        //开启配置
        boolean flag2=false;
            List<AttConfig> list = new ArrayList<>();
            list = service.findAll();
            for (AttConfig attConfig : list) {
                if (attConfig.getStatus() == 1) {
                    //把原先开启的关闭
                    service.update(attConfig.getC_id(), 0);
                    log.info("关闭原先的配置");
                    }
                if (attConfig.getC_id() == c_id) {
                    //把需要开启的开启
                    flag2=service.update(c_id, 1);
                    log.info("开启新配置");
                }
            }
            return new JsonReturn(flag2);
        }



}